<?php
/**
 * 重写Model
 * @author Ice iceinto@mallog.com.cn
 *
 */
class BaseModel extends Model
{
	/**
	 * 返回参数
	 * @var Array 返回参数
	 * @example
	 * $this->status['status'] = 1;
	 * $this->status['doc'] = '添加成功！';
	 *
	 * $this->status['status'] = 0;
	 * $this->status['doc'] = '添加失败！';
	 * @author Ice iceinto@mallog.com.cn
	 */
	protected $status = array();

	/**
	 * 查询基本信息单表
	 * @param Array $map 查询条件
	 * @param Boolean $more 单查询/多查询
	 * @param String $field 过滤字段默认：*
	 * @param String $limit 分页
	 * @param String $order 排序
	 */
	public function getBaseData($map = array(), $more = false, $field = '*', $limit = '',
			$order = '')
	{
		if ($more) {
			return $this->where($map)
			->field($field)
			->order($order)
			->limit($limit)
			->findAll();
		} else {
			return $this->where($map)
			->field($field)
			->order($order)
			->limit($limit)
			->find();
		}
	}

	/**
	 * 数据库中字段唯一性判断
	 * @param String $data 信息
	 * @param String $columns 字段名称
	 * @param String $mes 提示信息前缀
	 * @param Boolean $isMust 是否是必须：默认true
	 * @param Array $exclude 排除对比参数
	 * @author Ice iceinto@mallog.com.cn
	 *
	 */
	public function theOnly($data, $columns, $mes, $isMust = true,
			$exclude = array(), $operator = 'neq')
	{
		$this->status['status'] = 0;
		if ($isMust) {
			//数据判断
			if (!$data) {
				$this->status['doc'] = $mes . '必须填写！';
				return $this->status;
			}
		}
		//唯一性判断
		$map = array();
		$map[$columns] = $data;
		foreach ($exclude as $k => $v) {
			if ($v) {
				$map[$k] = array($operator, $v);
			}
		}
		$only = $this->where($map)->find();
		//dump($this->getLastSql());
		if ($only) {
			$this->status['doc'] = $mes . '已存在，请换一个' . $mes . '！';
		} else {
			$this->status['status'] = 1;
		}
		return $this->status;
	}

	/**
	 * 获取分页信息
	 * @param Int $count 总条数
	 * @author Ice iceinto@mallog.com.cn
	 * @return class Page
	 */
	protected function getPage($count)
	{
		import("@.ORG.Util.Page");
		return new Page($count, 20);
	}

	/**
	 * 基本添加处理
	 * @author Ice iceinto@mallog.com.cn
	 */
	protected function BaseAdd($data)
	{
		if ($this->status['status']) {
			if ($this->add($data)) {
				$this->status['status'] = 1;
				$this->status['doc'] = '添加成功！';
			} else {
				$this->status['status'] = 0;
				$this->status['doc'] = '添加失败！';
			}
		}
	}

	/**
	 * 基本修改处理
	 * @author Ice iceinto@mallog.com.cn
	 */
	protected function BaseUpdate($data)
	{
		if ($this->status['status']) {
			if ($this->save($data)) {
				$this->status['status'] = 1;
				$this->status['doc'] = '修改成功！';
			} else {
				$this->status['status'] = 0;
				$this->status['doc'] = '修改失败！';
			}
		}
	}

	public function logWrite($type, $log)
	{
		$table = $this->getTableName();
		$LogModel = D('Log');
		$data = $LogModel->logTemplate($type, $table, $log);
		$data = $LogModel->create($data);
		if ($data) {
			$LogModel->add($data);
		}
	}

	/**
	 * @see Model::add()
	 */
	public function add($data = '', $options = array(), $replace = false)
	{
		$result = parent::add($data, $options, $replace);
		if ($result) {
			//进行修改日志记录
			$log['sql'] = $this->getLastSql();
			$log['key'] = intval($result);
			$this->logWrite(1, $log);
		}
		return $result;
	}

	/**
	 * @see Model::save()
	 */
	public function save($data = '', $options = array())
	{
		$userInfo = getLoginUserInfo();
		$result = parent::save($data, $options);
		if ($result && $userInfo['id']!=1) {
			//进行日志记录
			$log['sql'] = $this->getLastSql();
			$log['key'] = intval($this->data[$this->getPk()]);
			$this->logWrite(2, $log);
		}
		return $result;
	}

	/**
	 * @see Model::delete()
	 */
	public function delete($data = '', $options = array())
	{
		$userInfo = getLoginUserInfo();
		$result = parent::delete($data, $options);
		if ($result && $userInfo['id']!=1) {
			//进行日志记录
			$log['sql'] = $this->getLastSql();
			$log['key'] =  intval($this->data[$this->getPk()]);
			$this->logWrite(3, $log);
		}
		return $result;
	}

	/**
	 * 根据类型获取图片完整显示
	 * @param String $type 上传类型
	 * @param boolean $isDate 是否有时间设置
	 * @return String 图片地址
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getManagedUrl($id, $type = '', $isDate = false)
	{
		$defaultImages = UPLOAD_URL . 'default.jpg';
		$FileManagedModel = M('FileManaged' . ucwords($type));
		$map = array();
		if ($id) {
			$map['id'] = $id;
			$fImages = $FileManagedModel->where($map)->find();
			if ($fImages) {
				if ($type && !$isDate) {
					return UPLOAD_URL . $type . '/' . $fImages['uri'];
				} else if ($type && $isDate) {
					//解析时间
					$year = strftime('%Y', $fImages['time']);
					$date = strftime('%m%d', $fImages['time']);
					//返回时间地址
					return UPLOAD_URL . $type . '/' . $year .
					'/' . $date . '/' . $fImages['uri'];
				} else {
					return UPLOAD_URL . $fImages['uri'];
				}
			}
			return $defaultImages;
		}
		return $defaultImages;
	}
}